﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Utility;
using System.Utility.DBMS;
using System.Utility.Messaging;
using System.Utility.UI;


namespace GuessLAB.UI
{

    public partial class SalesReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int startyear = 2015;
                int currentyear = startyear + 1;
                for (int i = 2000; i < currentyear; i++)
                {
                    ddlYear.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                }

                for (int i = 2000; i < currentyear; i++)
                {
                    ddlFiscalYer.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
                }
                

                string[] months = new string[] { "January", "Febraury", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" };

                for (int i = 0; i < months.Length; i++)
                {
                    ListItem monthItem1 = new ListItem();
                    monthItem1.Text = months[i];

                    monthItem1.Value = (i + 1).ToString();
                    ddlMonth.Items.Add(monthItem1);

                }


                DataTable dt = DBHelper.GetDataTable("GetAllAvailableFiscalNumPeriod", null).Tables[0];
                ddlFisclNum.DataSource = dt;
                ddlFisclNum.DataValueField = "FiscanNumId";
                ddlFisclNum.DataTextField = "FiscanNumValue";
                ddlFisclNum.DataBind();


                ddlRegion.DataSource = DBHelper.GetDataset("GetAllRegion", null).Tables[0];
                ddlRegion.DataValueField = "RegionId";
                ddlRegion.DataTextField = "RegionName";
                ddlRegion.DataBind();

                ddlCity.DataSource = DBHelper.GetDataset("GetAllCity", null).Tables[0];
                ddlCity.DataValueField = "CityId";
                ddlCity.DataTextField = "CityName";
                ddlCity.DataBind();

                DateTime currenttime = DateTime.Now;
                ddlYear.SelectedValue = currenttime.Year.ToString();
                ddlMonth.SelectedValue = (currenttime.Month-1).ToString();
            }
           
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSummary_Click(object sender, EventArgs e)
        {
            Bind();
        }


        protected void btnExport_Click(object sender, EventArgs e)
        {
            DataSet set = DBHelper.GetDataset("GetAllSalesExcel", new object[] { ddlMonth.SelectedValue, ddlYear.SelectedValue });
            DataTable table = set.Tables[0];


            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls"));
            Response.ContentType = "application/ms-excel";
            DataTable dt = table;
            string str = string.Empty;
            foreach (DataColumn dtcol in dt.Columns)
            {
                Response.Write(str + dtcol.ColumnName);
                str = "\t";
            }
            Response.Write("\n");
            foreach (DataRow dr in dt.Rows)
            {
                str = "";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Response.Write(str + Convert.ToString(dr[j]));
                    str = "\t";
                }
                Response.Write("\n");
            }
            Response.End();


        }
        


        #region grid


        public void Bind()
        {
            if (rdlType.SelectedValue == "1")
            {
                grdList.DataSource = DBHelper.GetDataset("GetAllSales", new object[] { ddlMonth.SelectedValue, ddlYear.SelectedValue,ddlRegion.SelectedValue, ddlCity.SelectedValue, "1" });
                grdList.DataBind();
            }
            else if (rdlType.SelectedValue == "2")
            {
                grdList.DataSource = DBHelper.GetDataset("GetAllSales", new object[] { ddlFisclNum.SelectedValue, ddlFiscalYer.SelectedValue, ddlRegion.SelectedValue, ddlCity.SelectedValue, "2" });
                grdList.DataBind();
            }
        }

        protected void grdList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdList.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdList.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                GridViewRow row = grdList.Rows[e.RowIndex];
                //factory.DeleteSource(row);
            }
            catch (Exception ex)
            {
                string msgId = MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, msgId);
            }
            Bind();
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Store_Code")
            {
                Session["SelectesStore"] = e.CommandArgument;
                Response.Redirect("Home.aspx");
            }
        }


        protected void grdList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdList.EditIndex = -1;
            Bind();
        }

        protected void grdList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = grdList.Rows[e.RowIndex];
            //factory.UpdateSource(row);
            grdList.EditIndex = -1;
            Bind();
        }

        protected void grdList_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            //GridViewRow row = grdList.Rows[e.NewValues];

            //grdList.EditIndex = -1;
            //Bind();

        }
        #endregion

        protected void rdlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadioButtonList list = (RadioButtonList)sender;
            if (list.SelectedValue == "1")
            {
                divFiscal.Visible = false;
                divGeneral.Visible = true;
            
            }
            else if  (list.SelectedValue == "2")
            {
                divGeneral.Visible = false;
                divFiscal.Visible = true;
            }
        }
    }
}
